package com.reactim.message.repository;

import com.reactim.message.entity.MessageReadStatus;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
import org.springframework.stereotype.Repository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * 消息读取状态数据访问接口
 */
@Repository
public interface MessageReadStatusRepository extends ReactiveMongoRepository<MessageReadStatus, String> {
    
    /**
     * 根据消息ID查找读取状态
     */
    Mono<MessageReadStatus> findByMessageId(String messageId);
    
    /**
     * 查找用户已读的消息
     */
    @Query("{'readStatus.userId': ?0}")
    Flux<MessageReadStatus> findByUserId(Long userId);
    
    /**
     * 检查用户是否已读消息
     */
    @Query("{'messageId': ?0, 'readStatus.userId': ?1}")
    Mono<MessageReadStatus> findByMessageIdAndUserId(String messageId, Long userId);
    
    /**
     * 统计消息的已读用户数量
     */
    @Query(value = "{'messageId': ?0}", fields = "{'readStatus': 1}")
    Mono<MessageReadStatus> findReadStatusByMessageId(String messageId);
}